package com.rtx.edi.platform.dash.error.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.api.annotation.DataSourceAnnotation;
import com.rtx.edi.model.base.Constant;
import com.rtx.edi.model.error.po.ApiSkuError;
import com.rtx.edi.platform.dash.error.mapper.PlApiSkuErrorMapper;
import com.rtx.edi.platform.dash.error.service.PlSkuErrorService;
import com.rtx.wms.customer.k3.base.service.impl.MaterialHandle;

@Service
public class PlSkuErrorServiceImpl extends ServiceImpl<PlApiSkuErrorMapper,ApiSkuError> implements PlSkuErrorService {
	
	@Autowired
	MaterialHandle materialHandle;

	@DataSourceAnnotation
	@Override
	public void selectApiSkuError(String datasourcekey,Page<ApiSkuError> pageParam, ApiSkuError error) {
		
		if(StringUtils.hasLength(error.getEdiflag())) {
			error.setEdiflag(error.getEdiflag());
		}else {
			error.setEdiflag("PF");
		}
		LambdaQueryWrapper<ApiSkuError> query=Wrappers.lambdaQuery();
		query
		.eq(ApiSkuError::getEdiflag, error.getEdiflag());
		if(error!=null) {
			query.eq( error.getSerialkey()!=null,ApiSkuError::getSerialkey, error.getSerialkey())
			.eq( error.getWhseid()!=null,ApiSkuError::getWhseid, error.getWhseid())
			.eq( error.getSku()!=null,ApiSkuError::getSku, error.getSku())
			.eq(error.getDescr()!=null,ApiSkuError::getDescr, error.getDescr())
			;
		}
		baseMapper.selectPage(pageParam,query);
		
	}

	@DataSourceAnnotation
	@Override
	public void reHandle(String whseid, ApiSkuError apiSkuError) {
		LambdaUpdateWrapper<ApiSkuError> update =Wrappers.lambdaUpdate();
		update.eq(ApiSkuError::getSerialkey,apiSkuError.getSerialkey())
		.set(ApiSkuError::getEdiflag, Constant.DEFAULT_FLAG);
		baseMapper.update(null, update);
	}

	@DataSourceAnnotation
	@Override
	public String requestK3SKU(String whseid, String requestStr)  throws Exception {
		return materialHandle.queryMaterial(requestStr);
	}

	@DataSourceAnnotation
	@Override
	public ApiSkuError getById(String whseid, String id) {
		return baseMapper.selectById(id);
	}
}
